import logging

import pytest

from resource.pom.clock import Clock
from utils.manager import Images


class Test:
    app = Clock

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        for i in range(4):
            logging.info('步骤1.{}：打开或反复启动时钟应用'.format(i))
            device.start_ability(self.app.bundle_name, self.app.ability_name)
            logging.info('预期结果1.{}：能正常打开，表针运行正常，与系统时间相对应'.format(i))
            device.assert_current_window(self.app.window_name)
            device.go_back()
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        crop_area = device.get_element_area(device.get_element_by_type('Canvas'))
        clock_pic0 = device.save_snapshot_to_local('clock0.jpeg')
        Images.crop_picture(clock_pic0, *crop_area)
        device.wait(5)
        clock_pic1 = device.save_snapshot_to_local('clock1.jpeg')
        # device.
        Images.crop_picture(clock_pic1, *crop_area)
        logging.info('预期结果2：表针运行正常')
        similarity = Images.compare_image_similarity(clock_pic0, clock_pic1)
        # print('====================', Images.get_image_datetime(clock_pic1))
        assert 0.8 < similarity < 1, '时钟未正常运转'

